#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "modbusmaster.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    modbusMaster=NULL;


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    //write
   // RegisterType type, int newStartAddress, quint16 newValueCount
      QModbusDataUnit::RegisterType type=QModbusDataUnit::Coils;
      int newStartAddress=0;
      QVector<quint16> newValues;
      newValues.resize(5);
      newValues[0]=1;
      newValues[1]=0;
      newValues[2]=1;
      newValues[3]=0;
      newValues[4]=1;

    QModbusDataUnit modbusDateUnit(type,newStartAddress,newValues);
    if(READ_WRITE_RESULT_STATE_WAITING==modbusMaster->writeModbusDataUnit(modbusDateUnit,1))
    {
      qDebug()<<"write READ_WRITE_RESULT_STATE_WAITING";
      QMessageBox::information(NULL, "information", "write READ_WRITE_RESULT_STATE_WAITING！", QMessageBox::Yes );
    }
    else
    {
       qDebug()<<"write failed";
       QMessageBox::information(NULL, "information", "write failed！", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    //read
    QModbusDataUnit::RegisterType type=QModbusDataUnit::Coils;
    int newStartAddress=0;
    QVector<quint16> newValues;
    newValues.resize(5);

  QModbusDataUnit modbusDateUnit(type,newStartAddress,newValues);
    if(1==modbusMaster->readModbusDataUnit(modbusDateUnit,1))
    {
      qDebug()<<"read READ_WRITE_RESULT_STATE_WAITING";
      QMessageBox::information(NULL, "information", "read READ_WRITE_RESULT_STATE_WAITING！", QMessageBox::Yes );
    }
    else
    {
       qDebug()<<"read failed 1";
       QMessageBox::information(NULL, "information", "read failed ！", QMessageBox::Yes );
    }

}

void MainWindow::on_pushButton_3_clicked()
{
    //get read result

    QModbusDataUnit modbusDateUnit;
      if(READ_WRITE_RESULT_STATE_SUCCESS==modbusMaster->getReadModbusDataUnit(modbusDateUnit))
      {
        qDebug()<<"read success"<<modbusDateUnit.value(0)<<","
                  <<modbusDateUnit.value(1)<<","
                    <<modbusDateUnit.value(2)<<","
                      <<modbusDateUnit.value(3)<<","
                        <<modbusDateUnit.value(4)<<",";

      }
      else
      {
         qDebug()<<"read failed 2";
         QMessageBox::information(NULL, "information", "read failed 2！", QMessageBox::Yes );
      }

}

void MainWindow::on_pushButton_4_clicked()
{
    if(NULL!=modbusMaster)
    {
        delete modbusMaster;
    }
    ModbusConnection typeIn=Tcp;
    QString portNameIn="";
    SerialSettings serialSettingIn;
//    QString ip_port_in="127.0.0.1:567";
    QString ip_port_in=ui->lineEdit_ipAndPort->text();
//    QString ip_port_in="10.1.21.15:567";
    modbusMaster = new ModbusMaster(typeIn,portNameIn,serialSettingIn,ip_port_in);


    //connect port
    if(1==modbusMaster->connectPort())
    {
        qDebug()<<"connect success";
        QMessageBox::information(NULL, "information", "connect success！", QMessageBox::Yes );

    }
    else
    {
        qDebug()<<"connect failed";
         QMessageBox::information(NULL, "information", "connect failed！", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_5_clicked()
{
    if(NULL==modbusMaster)
    {
        return ;
    }
    //disconnect
    if(1==modbusMaster->disConnectPort())
    {
        qDebug()<<"disconnect success";
        QMessageBox::information(NULL, "information", "disconnect success！", QMessageBox::Yes );

    }
    else
    {
        qDebug()<<"disconnect failed";
        QMessageBox::information(NULL, "information", "disconnect failed！", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_6_clicked()
{
    if(2==modbusMaster->getConnectState())
    {
        QMessageBox::information(NULL, "information", "已经连接", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "information", "未连接", QMessageBox::Yes );
    }
}

void MainWindow::on_pushButton_7_clicked()
{
    //write register
    // RegisterType type, int newStartAddress, quint16 newValueCount
       QModbusDataUnit::RegisterType type=QModbusDataUnit::HoldingRegisters;
       int newStartAddress=ui->lineEdit_writeRegisterAdd->text().toInt();
       QVector<quint16> newValues;
       newValues.resize(5);
       newValues[0]=ui->lineEdit->text().toInt();
       newValues[1]=111;
       newValues[2]=222;
       newValues[3]=444;
       newValues[4]=444;

     QModbusDataUnit modbusDateUnit(type,newStartAddress,newValues);
     if(READ_WRITE_RESULT_STATE_WAITING==modbusMaster->writeModbusDataUnit(modbusDateUnit,1))
     {
       qDebug()<<"write READ_WRITE_RESULT_STATE_WAITING";
     }
     else
     {
        qDebug()<<"write failed";
     }

}

void MainWindow::on_pushButton_8_clicked()
{
    //read register

    // RegisterType type, int newStartAddress, quint16 newValueCount
       QModbusDataUnit::RegisterType type=QModbusDataUnit::HoldingRegisters;
       int newStartAddress=ui->lineEdit_readRegisterAdd->text().toInt();
       QVector<quint16> newValues;
       newValues.resize(5);


     QModbusDataUnit modbusDateUnit(type,newStartAddress,newValues);
     if(READ_WRITE_RESULT_STATE_WAITING==modbusMaster->readModbusDataUnit(modbusDateUnit,1))
     {
       qDebug()<<"read READ_WRITE_RESULT_STATE_WAITING";
     }
     else
     {
        qDebug()<<"read failed";
     }

}

void MainWindow::on_pushButton_9_clicked()
{
    //read result
    QModbusDataUnit modbusDateUnit;
      if(READ_WRITE_RESULT_STATE_SUCCESS==modbusMaster->getReadModbusDataUnit(modbusDateUnit))
      {

          //QString str=modbusDateUnit.value(0);
          ui->lineEdit_readResult->setText(QString::number(modbusDateUnit.value(0)));
        qDebug()<<"read success"<<modbusDateUnit.value(0)<<","
                  <<modbusDateUnit.value(1)<<","
                    <<modbusDateUnit.value(2)<<","
                      <<modbusDateUnit.value(3)<<","
                        <<modbusDateUnit.value(4)<<",";

      }
      else
      {
         qDebug()<<"read failed 2";
      }
}

void MainWindow::on_pushButton_10_clicked()
{
    //read register by text_line

    QModbusDataUnit::RegisterType type=QModbusDataUnit::DiscreteInputs;
    int newStartAddress=ui->lineEdit_3->text().toInt();
    QVector<quint16> newValues;
    newValues.resize(ui->lineEdit_4->text().toInt());


  QModbusDataUnit modbusDateUnit(type,newStartAddress,newValues);
  if(READ_WRITE_RESULT_STATE_WAITING==modbusMaster->readModbusDataUnit(modbusDateUnit,1))
  {
    qDebug()<<"read READ_WRITE_RESULT_STATE_WAITING";
  }
  else
  {
     qDebug()<<"read failed";
  }

}

void MainWindow::on_pushButton_11_clicked()
{
    //write register by text_line

    QModbusDataUnit::RegisterType type=QModbusDataUnit::HoldingRegisters;
    int newStartAddress=ui->lineEdit_3->text().toInt();
    QVector<quint16> newValues;
    newValues.resize(ui->lineEdit_4->text().toInt());

    int count=newValues.size();
    for(int i=0;i<count;i++)
    {
        newValues[i]=0;
    }
    newValues[0]=ui->lineEdit_5->text().toInt();


  QModbusDataUnit modbusDateUnit(type,newStartAddress,newValues);
  if(READ_WRITE_RESULT_STATE_WAITING==modbusMaster->writeModbusDataUnit(modbusDateUnit,1))
  {
    qDebug()<<"write READ_WRITE_RESULT_STATE_WAITING";
  }
  else
  {
     qDebug()<<"write failed";
  }
}

void MainWindow::on_pushButton_12_clicked()
{
    //get read result
    QModbusDataUnit modbusDateUnit;
      if(READ_WRITE_RESULT_STATE_SUCCESS==modbusMaster->getReadModbusDataUnit(modbusDateUnit))
      {

          //QString str=modbusDateUnit.value(0);
          ui->lineEdit_5->setText(QString::number(modbusDateUnit.value(0)));
        qDebug()<<"read success";
        int count=modbusDateUnit.valueCount();
        for(int i=0;i<count;i++)
        {
            qDebug()<<"modbusDateUnit.value(i)"<<i<<"="<<modbusDateUnit.value(i);
        }

      }
      else
      {
         qDebug()<<"read failed 2";
      }
}
